import {ref, computed} from 'vue'
import {defineStore} from 'pinia'

export const constantStore = defineStore('constant', () => {
    let imageUrl = ref();
    const doubleCount = computed(() => count.value * 2);
    let webTitle = ref("")
    let pageSizes = ref([20, 50, 100, 200, 500])
    let stateMapping = [
        {type: "success", data: "0", msg: "正常",},
        {type: "delete", data: "1", msg: "回收站",}]
    let stateOption = [
        {
            label: "是",
            value: "1"
        },
        {
            label: "否",
            value: "0"
        }
    ]

    function setWebTitle(title) {
        webTitle.value = title
    }

    function checkPasswordStrength(password) {
        // 默认密码强度为空
        let power = -1;
        if (/[0-9]/.test(password) && /[a-z]/.test(password) && /[A-Z]/.test(password) && /[^a-zA-Z0-9]/.test(password)) {
            // 如果密码包含数字、小写字母、大写字母和特殊字符中的至少三种类型，强度提升为强
            return 2;
        } else if (password.length >= 8) {// 如果密码长度大于等于8个字符，强度提升为中
            power = 1;
        } else if (password !== "") {
            power = 0;
        }
        return power;
    }

    function itemIsNull(item) {
        return item === '' || item === null || item === undefined
    }

    function copyPage(pageObj, resData) {
        pageObj.value.currentPage = resData.page;
        pageObj.value.pageSize = resData.size;
        pageObj.value.totalPages = resData.totalPages;
        pageObj.value.totalCount = resData.totalCount;
        pageObj.value.first = resData.first;
        pageObj.value.last = resData.last;
    }

    function listToSelect(dataList, label, value) {
        let select = [];
        for (let i = 0; i < dataList.length; i++) {
            let obj = {
                label: dataList[i][label],
                value: dataList[i][value]
            }
            select.push(obj);
        }
        return select;
    }

    function listToTag(dataList, msg, data) {
        let select = [];
        for (let i = 0; i < dataList.length; i++) {
            let obj = {
                type: "primary",
                msg: dataList[i][msg],
                data: dataList[i][data]
            }
            select.push(obj);
        }
        return select;
    }

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    return {
        webTitle,
        setWebTitle,
        checkPasswordStrength,
        sleep,
        itemIsNull,
        copyPage,
        listToSelect,
        listToTag,
        stateMapping,
        stateOption,
        imageUrl,
        pageSizes
    }
}, {
    persist: {
        enabled: true, //Store中数据持久化生效
    },
})
